<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 自定义组件，标题 -->
      <PageTitle :show-before="true">
        <template v-slot:before>
          <h2>意见建议</h2>
        </template>
      </PageTitle>
      <el-row :gutter="10" style="margin-top: 10px">
        <el-col :span="3">
          <el-select
            placeholder="不限所属类型"
            clearable
            v-model="selProposalQuery.type"
            @change="handleBelongToChange"
          >
            <el-option
              v-for="item in belongTo"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-select
            placeholder="不限处理状态"
            clearable
            @change="handleStatusChange"
            v-model="selProposalQuery.status"
          >
            <el-option
              v-for="item in handleStatus"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
      </el-row>
      <!-- 数据表 -->
      <el-table
        v-loading="downloadLoading"
        ooltip-effect="dark"
        :data="selProposalLists"
        max-height="500px"
        style="margin-top: 20px"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" fixed />
        <el-table-column
          label="类型"
          align="center"
          width="width"
          prop="type_cn"
          fixed
        >
          <template slot-scope="scope">
            <el-tag :type="getTypeTag(scope.row.type)">
              {{
                scope.row.type === 1
                  ? "建议"
                  : scope.row.type === 2
                  ? "意见"
                  : scope.row.type === 3
                  ? "求助"
                  : scope.row.type === 4
                  ? "投诉"
                  : "未知"
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="处理情况" align="center" prop="full_name" fixed>
          <template slot-scope="scope">
            <el-tag :type="scope.row.status ? 'success' : 'danger'">{{
              scope.row.status ? "已处理" : "未处理"
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="内容"
          align="center"
          prop="content"
          width="300"
          fixed
        />
        <el-table-column
          label="联系方式"
          align="center"
          prop="mobile"
          width="180"
        >
        </el-table-column>
        <el-table-column label="添加时间" align="center" prop="addtime">
          <template slot-scope="scope">
            {{ scope.row.addtime | dateFormat }}
          </template>
        </el-table-column>

        <el-table-column width="150" align="center" fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="delBtn(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-row :gutter="10" style="margin-top: 20px">
        <el-col :span="8">
          <el-button type="primary" size="small" @click="handleClick"
            >处理</el-button
          >
          <el-button type="danger" size="small" @click="handleBatchDelete"
            >删除所选</el-button
          >
        </el-col>
        <el-col :span="16">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage4"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            style="text-align: right"
          >
          </el-pagination>
        </el-col>
      </el-row>
    </div>

    <!-- 处理弹层 -->
    <el-dialog
      title="将所选信息设置为"
      :visible.sync="handleDialog"
      width="25%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="处理情况">
          <el-radio v-model="radio" label="0">未处理</el-radio>
          <el-radio v-model="radio" label="1">已处理</el-radio>
        </el-form-item>
        <el-row :gutter="10" type="flex" justify="end">
          <el-button @click="handleDialog = false">取消</el-button>
          <el-button type="primary" @click="btnOk">确定</el-button>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  selProposalListApi,
  delProposalByIdsApi,
  updateStatusByIdsApi,
} from "@/api/manageUser";
export default {
  data() {
    return {
      handleDialog: false,
      radio: "0",
      selProposalQuery: {
        page: 1,
        pageSize: 10,
        type: "",
        status: "",
      },
      ids: [],
      selectedStatuses: [],
      selProposalLists: [],
      belongTo: [
        { value: "", name: "不限所属类型" },
        { value: 1, name: "建议" },
        { value: 2, name: "意见" },
        { value: 3, name: "求助" },
        { value: 4, name: "投诉" },
      ],
      handleStatus: [
        { value: "", name: "不限处理状态" },
        { value: 1, name: "已处理" },
        { value: 0, name: "未处理" },
      ],
      selectedIds: [],
    };
  },
  created() {
    this.selProposalList();
  },
  methods: {
    getTypeTag(type) {
      switch (type) {
        case 1:
          return "success"; // 建议 - 绿色
        case 2:
          return "warning"; // 意见 - 黄色
        case 3:
          return "info"; // 求助 - 灰色
        case 4:
          return "danger"; // 投诉 - 红色
        default:
          return "";
      }
    },
    // 所属类型改变
    handleBelongToChange(value) {
      console.log(value);
      this.selProposalQuery.page = 1;
      this.selProposalList();
    },
    // 处理状态改变
    handleStatusChange(value) {
      console.log(value);
      this.selProposalQuery.page = 1;
      this.selProposalList();
    },
    // 选择每页显示多少条数据的时候
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.selProposalQuery.pageSize = pageSize;
      this.selProposalList();
    },
    // 选择当前页的时候
    handleCurrentChange(page) {
      console.log(page);
      this.selProposalQuery.page = page;
      this.selProposalList();
    },
    // 审核
    handleClick() {
      if (this.selectedIds.length === 0) {
        this.$message.warning("请选择要处理的数据");
        return;
      }
      this.handleDialog = true;
    },
    // 删除
    delBtn(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            console.log(id);
            console.log(this.selectedIds);
            this.selectedIds.push(id);
            const res = await delProposalByIdsApi(this.selectedIds);
            console.log(res);
            if (res.code === 1) {
              this.$message.success("删除成功!");
              this.selectedIds = [];
              this.selProposalList();
            } else {
              this.$message.error("删除失败!");
            }
          } catch {
            this.$message.error("删除失败!");
          }
        })
        .catch(() => {
          // this.$message({
          //   type: "info",
          //   message: "已取消删除",
          // });
        });
    },
    async selProposalList() {
      const res = await selProposalListApi(this.selProposalQuery);
      console.log(res);
      this.selProposalLists = res.records;
      this.total = res.total;
    },
    handleSelectionChange(selection) {
      this.selectedIds = selection.map((item) => item.id);
      this.selectedStatuses = selection.map((item) => item.status);
    },
    // 点击确定
    async btnOk() {
      try {
        console.log(this.selectedIds);
        const res = await updateStatusByIdsApi(this.selectedIds, this.radio);
        console.log(res);
        if (res.code === 1) {
          this.$message.success("处理成功!");
          this.selectedIds = [];
          this.handleDialog = false;
          this.selProposalList();
        } else {
          this.$message.error("处理失败");
        }
      } catch {
        this.$message.error("处理失败");
      }
    },
    // 批量删除
    handleBatchDelete() {
      if (this.selectedIds.length === 0) {
        this.$message.warning("请选择要删除的数据");
        return;
      }

      this.$confirm("此操作将永久删除选中的数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            const res = await delProposalByIdsApi(this.selectedIds);
            if (res.code === 1) {
              this.$message.success("删除成功!");
              this.selectedIds = [];
              this.selProposalList();
            } else {
              this.$message.error(res.msg || "删除失败");
            }
          } catch (error) {
            console.error("删除失败:", error);
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          // 取消删除操作
        });
    },
  },
};
</script>

<style scoped></style>
